package thinker.miracle.utils;

import java.security.MessageDigest;

/**
 * @author bo.chen
 *
 */
public class MD5 {

	/**
	 * md5加密
	 * 
	 * @param code
	 *            原文
	 * @return 密文
	 * @throws Exception
	 */
	public static String md5(String code) throws Exception {
		try {
			if (code == null)
				throw new RuntimeException("code can not be null");
			MessageDigest md = MessageDigest.getInstance("MD5");
			md.update(code.getBytes());
			byte b[] = md.digest();
			int i;
			StringBuffer buf = new StringBuffer("");
			for (int offset = 0; offset < b.length; offset++) {
				i = b[offset];
				if (i < 0)
					i += 256;
				if (i < 16)
					buf.append("0");
				buf.append(Integer.toHexString(i));
			}
			code = buf.toString();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		return code;
	}
}

